<?php

namespace app\controller\property\bill;

use app\common\repositories\bill\BillFinanceRepository;
use app\common\repositories\property\basic\PropertyBasicRepository;
use think\App;
use app\controller\property\Base;
use app\validate\bill\BillCourseValidate;
use app\common\repositories\bill\BillCourseRepository;
use app\common\repositories\property\PropertyRepository;

class BillCourse extends Base
{
    protected $repository;

    public function __construct(App $app, BillCourseRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        if (!$this->request->isAjax()) {
            $this->assign([
                'addAuth' => property_auth('propertyBillCourseAdd'),
                'editAuth' => property_auth('propertyBillCourseEdit'),
                'delAuth' => property_auth('propertyBillCourseDel'),
            ]);
        }
    }

    protected function commonParams()
    {
        /**@var PropertyBasicRepository $propertyBasicRepository */
        $propertyBasicRepository = app()->make(PropertyBasicRepository::class);
        $propertyData = $propertyBasicRepository->getPropertyData($this->request->propertyId, ['is_shop' => 1]);

        /**@var BillFinanceRepository $billFinanceRepository */
        $billFinanceRepository = app()->make(BillFinanceRepository::class);
        $payMethodData = $billFinanceRepository::PAY_METHOD;
        $payTypeData = $billFinanceRepository::PAY_TYPE;
        $this->assign([
            'payTypeData' => $payTypeData,
            'propertyData' => $propertyData,
            'payMethodData' => $payMethodData,
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'bill_id' => '',
                'check_type' => '',
                'pay_type' => '',
                'pay_method' => '',
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        $this->commonParams();
        return $this->fetch('bill/course/list', [
            'detailAuth' => property_auth('propertyBillCourseDetail'),
        ]);
    }

    public function stayCheckList()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'bill_id' => '',
                'keywords' => '',
            ]);
            $where['check_type'] = 1;
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        return $this->fetch('bill/course/check_list');
    }


    public function checkAdd()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'check_type' => '',
                'check_amount' => '',
                'check_remark' => ''
            ]);
            validate(BillCourseValidate::class)->scene('checkAdd')->check($param);
            try {
                $res = $this->repository->checkPayMomey($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('操作成功');
                } else {
                    return $this->success('操作失败');
                }
            } catch (\Exception $e) {
                exception_log('操作收款确认失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('bill/course/check_info', [
                'info' => $info,
            ]);
        }
    }

    public function stayAffirmList()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'bill_id' => '',
                'keywords' => '',
            ]);
            $where['affirm_type'] = 1;
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        return $this->fetch('bill/course/affirm_list');
    }


    public function affirmAdd()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'affirm_type' => '',
                'affirm_amount' => '',
                'affirm_remark' => ''
            ]);
            if ($info['check_type'] != 9) {
                return $this->success('非法操作');
            }
            validate(BillCourseValidate::class)->scene('affirmAdd')->check($param);
            try {
                $res = $this->repository->affirmPayMomey($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('操作成功');
                } else {
                    return $this->success('操作失败');
                }
            } catch (\Exception $e) {
                exception_log('操作收款确认失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('bill/course/affirm_info', [
                'info' => $info,
            ]);
        }
    }

    public function detail()
    {
        $id = (int)$this->request->param('id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if ($info['id'] <= 0) {
            return $this->error('支付记录不存在');
        }
        $this->commonParams();
        return $this->fetch('bill/course/detail', [
            'info' => $info,
        ]);
    }


}